# -*- codeing : utf-8 -*-

############################################################################
# Name : 
# Author: Manabu Terada
# Date: Oct/11/2008
# Last modified: 
# E-mail: terada@cmscom.jp
#############################################################################

from Products.CMFCore.utils import getToolByName

from Products import UnicodeSplitterPatch
import base

class TestReplaceSplitter(base.BigramSplitterTestCase):
    """ Install basic test """ 
    def afterSetUp(self):
        pass

    def testReplaceSplitter(self):
        from Products.CMFPlone.UnicodeSplitter import Splitter
        self.failUnless(Splitter.meta_type == "BigramSplitter")

class TestSearchingEnglish(base.BigramSplitterTestCase):
    """ Install English test """ 
    def afterSetUp(self):
        self.setRoles(('Manager',))
        self.portal.invokeFactory('Document', 'doc1')
        self.doc1 = getattr(self.portal, 'doc1')
        self.doc1.setTitle("Plone is nice!")
        self.doc1.setText("Test pages.")
        self.doc1.reindexObject()

    def testSearch(self):
        catalog = getToolByName(self.portal, 'portal_catalog')
        items1 = catalog(SearchableText="Test")
        self.failUnless(len(items1) == 1)
        items12 = catalog(SearchableText="Pages")
        self.failUnless(len(items12) == 1)
        self.portal.manage_delObjects(['doc1'])
        items2 = catalog(SearchableText="Test")
        self.failUnless(len(items2) == 0)

class TestSearchingJapanese(base.BigramSplitterTestCase):
    """ Install Japanese test """ 
    def afterSetUp(self):
        self.setRoles(('Manager',))
        self.portal.invokeFactory('Document', 'doc1')
        self.doc1 = getattr(self.portal, 'doc1')
        self.doc1.setTitle("Ploneは素晴らしい。")
        self.doc1.setText("このページは予想している通り、テストです。 Pages Testing.")
        self.doc1.reindexObject()

    def testSearch(self):
        catalog = getToolByName(self.portal, 'portal_catalog')
        items1 = catalog(SearchableText="予想")
        self.failUnless(len(items1) == 1)
        items12 = catalog(SearchableText="素晴らしい")
        self.failUnless(len(items12) == 1)
        items13 = catalog(SearchableText="Pages")
        self.failUnless(len(items13) == 1)
        self.portal.manage_delObjects(['doc1'])
        items2 = catalog(SearchableText=u"予想")
        self.failUnless(len(items2) == 0)
        ## 既知のバグ　カタカナ長音対応  &  "。"の後の英文字が検索出来ない

def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(TestReplaceSplitter))
    suite.addTest(makeSuite(TestSearchingEnglish))
    suite.addTest(makeSuite(TestSearchingJapanese))
    return suite
